package com.ruyuan.seckilling.activity.controller;

import com.ruyuan.seckilling.activity.cache.ActivityCacheSupport;
import com.ruyuan.seckilling.activity.entity.Activity;
import com.ruyuan.seckilling.activity.service.ActivityService;
import com.ruyuan.seckilling.common.MapResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author zhonghuashishan
 */
@Slf4j
@RestController
@RequestMapping("/activity/frontend")
public class FrontendActivityController {

    @Autowired
    private ActivityCacheSupport activityCacheSupport;

    /**
     * 前台查询秒杀活动
     * curl http://localhost:7001/f/activity/queryList
     */
    @GetMapping("/queryList")
    public MapResult queryList() {
        List<Activity> activities = activityCacheSupport.queryShowableActivity();
        if (CollectionUtils.isEmpty(activities)) {
            return MapResult.successResult().set("dataList", Collections.emptyList());
        }
        List<Object> dataList = new ArrayList<>();
        for (Activity activity : activities) {
            Map<String, Object> data = new LinkedHashMap<>();
            data.put("activityId", activity.getId());
            data.put("activityName", activity.getActivityName());
            data.put("showTime", activity.getShowTime());
            data.put("startTime", activity.getStartTime());
            data.put("endTime", activity.getEndTime());
            dataList.add(data);
        }
        return MapResult.successResult().set("data", dataList);
    }
}
